package com.hsq.ndmall.auth.component;

import com.hsq.ndmall.auth.dto.SecurityUser;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 黄四清
 * @version 1.0.0
 * @ClassName JwtTokenEnhancer.java
 * @Description TODO 你如果想往JWT添加自定义信息，比如用户登录的ID，实现TokenEnhancer接口
 * @createTime 2021年08月23日 17:11:00
 */

/**
 * JWT内容增强
 */
@Component
public class JwtTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
        // 得到用户的信息
        SecurityUser securityUser = (SecurityUser)oAuth2Authentication.getPrincipal();
        Map<String,Object> info = new HashMap<>();
        info.put("id",securityUser.getId());
        ((DefaultOAuth2AccessToken)oAuth2AccessToken).setAdditionalInformation(info);
        return oAuth2AccessToken;
    }
}
